<?
	class InputList extends Input
	{
		var $aData;
		var $nMaxLen;     // Limite de la longitud de caracteres de la descripcion ( 0 es sin limite )
		var $aColValue;     // El o los indices de la matriz que se usaran como valor
		var $aColDescription;     // El o los indices de la matriz que se usaran como descripcion
		var $cSeparatorValue;     // El caracter que separa cuando hay multiples valores
		var $cSeparatorDescription;     // El caracter que separa cuando hay multiples descripciones
		var $cOptions = 'L';
		var $cNewText = 'Otro';
		var $bAddNewItems = false;
		var $nqRows;
		var $cLanguage = '';

	function setAddNewItems( $cNewText = '')
		{
			$this->bAddNewItems = true;
			if ( $cNewText ) $this->cNewText = $cNewText;
		}
	function setRadio( $cRadioOptions ='D') // D: XXX O y I: O XXXX
		{
			$this->cOptions = $cRadioOptions;
		}
	function setLanguage( $cLanguage)
		{
			$this->cLanguage = $cLanguage;
		}
	function setRows( $nqRows )
		{
			$this->nqRows = $nqRows;
		}

	function setSeparatorDescription( $cSeparatorDescription )
		{
			$this->cSeparatorDescription = $cSeparatorDescription;
		}
	function setColValueMatrix( $aColValue )
		{
			$this->aColValue = $aColValue;
			// Si no tiene valor => busca si por Get vino uno valor
			if ( $this->cValue == '' )
			{
				$cValue = "";
				foreach( $aColValue as $cField )
				{
					$cValue .= $this->cSeparatorValue . getParam($cField);
				}
				$this->setValue( substr($cValue, strlen($this->cSeparatorValue) ) );
			}
		}
	function setColDescriptionMatrix( $aColDescription )
		{
			$this->aColDescription = $aColDescription;
		}
	function addColValue( $cColValue )
		{
			$this->aColValue[] =  $cColValue;
		}
	function addColDescription( $nColDescription )
		{
			$this->aColDescription[] = $nColDescription;
		}
	function setOptions( $cOptions )
		{
			$this->cOptions = $cOptions;
		}
	function setRange( $nDesde, $nHasta )
		{
			unset($this->aData);
			for( $nIndex = $nDesde; $nIndex <= $nHasta; $nIndex++)
			{
				$this->aData[] = array( $nIndex, $nIndex);
			}
		}
	// $cFilter: Son las extensiones validas separadas por un espacio. Por ej: "jpg gif jpeg tiff png" o "htm html php"
	function setDirectory( $cDirectory, $cFilter = '', $bRecursivo = false )
		{
			$aDirectorio = array($cDirectory);
			if ( $cFilter != '' ) $aFilter = explode ( ' ' , $cFilter );
			// Loop Por cada Directorio (en caso de ser recursivo =
			while ( count($aDirectorio) > 0 )
			{
				$cDirActual = array_pop($aDirectorio);
				$fd = opendir( $cDirActual );
				// Loop por cada archivo
				while ( $cArchivo = readdir($fd) )
				{
					$cArchivoActual = $cDirActual . "/" . $cArchivo;
					// Si es un Directorio y es Recursivo => lo Apila
					if ( filetype($cArchivoActual) == 'dir' && $bRecursivo == 1 )  array_push($aDirectorio, $cDirActual  . '/' .$cArchivo);
					// Si es un archivo
					if ( filetype($cArchivoActual ) == 'file' )
					{
						list ( $cNombre, $cExtension ) = explode( ".", $cArchivo );
						// Aplica el Filtro de extensiones si esta definido
						if ( $cFilter == '' || in_array( $cExtension, $aFilter) )
						{
							$this->aData[] = array( $cArchivo , $cNombre );
						}
					}
				}
				closedir($fd);
			}
		}
	function setBool (  )
		{
			unset($this->aData);
			$this->aData[] = array( 0x0, 'No' );
			$this->aData[] = array( 0x1, 'Si' );
		}
	function setMonth(  )
		{
			unset($this->aData);
			$this->aData[] = array( 1, 'Enero' );
			$this->aData[] = array( 2, 'Febrero' );
			$this->aData[] = array( 3, 'Marzo' );
			$this->aData[] = array( 4, 'Abril' );
			$this->aData[] = array( 5, 'Mayo' );
			$this->aData[] = array( 6, 'Junio' );
			$this->aData[] = array( 7, 'Julio' );
			$this->aData[] = array( 8, 'Agosto' );
			$this->aData[] = array( 9, 'Septiembre' );
			$this->aData[] = array( 10, 'Octubre' );
			$this->aData[] = array( 11, 'Noviembre' );
			$this->aData[] = array( 12, 'Diciembre' );
		}
	function addOption( $cKey, $cValue  )
		{
			$this->aData[] = array( $cKey, $cValue );
		}
	function getMatrix ( )
	{
		return $this->aData;
	}
	function setMatrix ( $aData )
		{
			$this->aData = $aData;
		}
	function setMaxLen( $nMaxLen )
		{
			$this->nMaxLen = $nMaxLen;
		}
	function InputList( $cLabel, $cField, $cValue = '', $bMandatory = false )
		{
			$this->Input($cLabel, $cField, $cValue, $bMandatory );
			// Setea la propiedades con los valores por omision
			$this->cEmptyText = '-';
			$this->aColValue[] = '0';
			$this->aColDescription[] = '1';
			$this->cSeparatorValue = '|';
			$this->cSeparatorDescription = ' ';
		}
	function makeRadio()
		{
			$cReturn = "";
			if ( $this->bMandatory && $this->cValue === "" )
			{
				$this->cValue = $this->aData[0][1];
			}
			
			$cReturn.= "<div style='overflow:auto'>";
			foreach ($this->aData as $aFila)
			{
				$cDataValue       = $aFila[0];
				$cDataDescription = $aFila[1];
				if ( $this->cLanguage ) $cDataDescription  = extractLanguage( $cDataDescription  , $this->cLanguage );
				$cChecked = ( $this->cValue == $cDataValue && strlen($this->cValue) > 0) ? " checked ": "";
				$cPropiedades = $this->getProperties();
				$cEventos     = $this->getEvents();
				if ( $this->cOptions == 'D' )
					$cFloat = "right";
				else
					$cFloat = "left";
					
				$cReturn.= "	<div style='float:$cFloat'><input $cEventos $cPropiedades type='radio' $cChecked id='" . $this->cField . "_" . $cDataValue . "' name='". $this->cField . "' value='$cDataValue'/></div>
							<div style='float:$cFloat; padding-top:3px'><span class='input_check'  onclick='javascript: oInput = document.getElementById( \"" . $this->cField . "_" . $cDataValue . "\" ); oInput.checked= true; if (oInput.onchange) oInput.onchange(); return false;'>$cDataDescription</span></div>";
			}
			$cReturn.= "</div>";
			return $cReturn;
		}
	function make( )
		{
			switch ( $this->cOptions )
			{
				default:
				case 'L' : return $this->makeList(); break;
				case 'R' : case 'D' : return $this->makeRadio(); break;
				case 'I' : return $this->makeRadio(); break;
			}
		}
	function getValueAsString ()
		{
			return $this->getDescription() ;
		}
	function setValueFromString ( $cValue )
		{
			if ( $cValue{0} == '=' || $cValue{0} == 'i' ||  is_numeric($cValue) )
				$this->setValue(  $cValue );
			else
				$this->setDescription(  $cValue );
		}
	function setValue ( $cValue )
	{
		if ( ord($cValue)  == 1 )	$cValue = "1" ;
		if ( ord($cValue)  == 0 && strlen($cValue) > 0 )	$cValue = 0 ;
		parent::setValue($cValue);
	}
	function makeList()
		{
			// Si esta setea como Oculto
				if ( $this->bHidden )  return parent::make();
			$cReturn = '';
			$cWidth = '';
			$cPropiedades = $this->getProperties();
			$cEventos     = $this->getEvents();
			if ( $this->cWidth ) $cWidth = "style='width: " . $this->cWidth . "px'";
			if ( $this->nqRows ) $cRows  = " size='" . $this->nqRows . "' ";
			// Arma el Principio del TAG SELECT
			$cReturn .= "<select $cWidth $cRows name='". $this->cField . "' $cPropiedades $cEventos id='". $this->cField . "'";
			// Arma el Fin del TAG SELECT
			$cReturn .= '>';
			// Si es obligatorio => Agrega un *
			if ( ! $this->bMandatory )
			{
				$cReturn .= "<option ";
				if ($this->cValue == "")
				{
					$cReturn .= " selected=\"selected\" ";
				}
				$cReturn .= " value=''>" . $this->cEmptyText . "</option>\r\n";
			}
			if ( is_array ( $this->aData ) )
			{
				// Por Cada elemento de la matriz agrega el tag OPTION
				foreach ($this->aData as $aFila)
				{
					$cDataValue = $cDataDescription = '';
					// Arma el Valor que queda en el option ( si hay muliples valores los concatena )
					foreach( $this->aColValue as $cColValue )
					{
						$cDataValue .= $aFila[$cColValue] . $this->cSeparatorValue;
					}
					$cDataValue = substr( $cDataValue, 0, - strlen( $this->cSeparatorValue ) );
					// Arma la Descripcion que se ve en el option ( si hay muliples valores los concatena )
					foreach( $this->aColDescription as $nColDescription )
					{
						if ( $this->cLanguage ) $cDataDescription  .= extractLanguage( $aFila[$nColDescription], $this->cLanguage ). $this->cSeparatorDescription;
						else $cDataDescription .= $aFila[$nColDescription] . $this->cSeparatorDescription;
					}
					$cDataDescription = substr( $cDataDescription, 0, - strlen( $this->cSeparatorDescription ) );
					// Agrega el Principio del option
					$cReturn .= "<option ";
					if ( strlen($this->cValue) > 0 ) {
						// Si es un Array el Valor seleccionado (es decir que hay multiples selecciones )
						if ( is_array( $this->cValue) )
						{
							if ( in_array( $cDataValue, $this->cValue ) )
							{
								$cReturn .= " selected=\"selected\" ";
							}
						} elseif ((strlen($this->cValue) > 0) && ($this->cValue == $cDataValue ))
						{
							$cReturn .= " selected=\"selected\" ";
						}
					} else
					{
						// Si no tiene asignado un valor y es mandatoy, entonces se queda con el primer registro
						if ( $this->bMandatory )
						{
							$this->cValue = $cDataValue;
						}
					}
					// Agrega el Valor del Option
					$cReturn .= "value='$cDataValue'>";
					// Si Tiene un Limite de caracteres en la Descripcion la recorta, y agrega la Descripcion
					if ( $this->nMaxLen > 0 )
					{
						$cReturn .= substr( $cDataDescription, 0, $this->nMaxLen );
					} else
					{
						$cReturn .= $cDataDescription;
					}
					// Agrega el final del option
					$cReturn .= "</option>\r\n";
				} // Fin por cada Fila de la Matriz
			}
			if ( $this->bAddNewItems )
			{
				$cOnclick = "cValue = prompt(\"Nuevo item\"); if ( cValue  ) {oOption = new Option( cValue, cValue );	try { this.parentNode.add(oOption, null ); } catch(ex) { this.parentNode.add(oOption, parent.selectedIndex); } oOption.selected = true;}";
				$cReturn .= "<option onclick='$cOnclick' value=''>" . $this->cNewText . "</option>\r\n";
			}

			$cReturn .= "</select>";
			return $cReturn;
		}
	function setDescription( $cDescripcion  )
		{
			if ( $this->aData )
			{
				foreach( $this->aData as $aFila )
				{
					// Arma la Descripcion ( si hay muliples valores los concatena )
					$cDataValue = $cDataDescription = '';
					foreach( $this->aColDescription as $nColDescription )
					{
						$cDataDescription .= $aFila[$nColDescription] . $this->cSeparatorDescription;
					}
					$cDataDescription = substr( $cDataDescription, 0, - strlen( $this->cSeparatorDescription ) );
					if ( $cDataDescription == $cDescripcion )
					{
						// Arma el Valor ( si hay muliples valores los concatena )
						foreach( $this->aColValue as $cColValue )
						{
							$cDataValue .= $aFila[$cColValue] . $this->cSeparatorValue;
						}
						$this->cValue = substr( $cDataValue, 0, - strlen( $this->cSeparatorValue ) );
					}
				}
			}
		}
	function getDescription( $cValue = '' )
		{
			// Por omision toma el value del objeto
			if ( !$cValue ) $cValue  = $this->cValue ;
			$nqColValues = count($this->aColValue);
			if ( $cValue && $this->aData )
			{
				foreach( $this->aData as $aFila )
				{
					// Arma el Valor ( si hay muliples valores los concatena )
					$cDataValue = $cDataDescription = '';
					if ( $nqColValues == 1 ) 
						$cDataValue = $aFila[0];
					else
					{
						foreach( $this->aColValue as $cColValue )
						{
							$cDataValue .= $aFila[$cColValue] . $this->cSeparatorValue;
						}
						$cDataValue = substr( $cDataValue, 0, - strlen( $this->cSeparatorValue ) );
					}
					if ( $cDataValue == $cValue )
					{
						// Arma la Descripcion ( si hay muliples valores los concatena )
						foreach( $this->aColDescription as $nColDescription )
						{
							$cDataDescription .= $aFila[$nColDescription] . $this->cSeparatorDescription;
						}
						$cDataDescription = substr( $cDataDescription, 0, - strlen( $this->cSeparatorDescription ) );
						return $cDataDescription;
					}
				}
			}
		}
	function setValueIndex( $nIndex, $cValue )
	{
		$aValue = explode ( $this->cSeparatorValue, $this->cValue );
		$aValue[$nIndex] = $cValue;
		$this->cValue = implode( $this->cSeparatorValue, $aValue );
	}
	function getValueIndex( $nIndex = 0)
		{
			$aValue = explode ( $this->cSeparatorValue, $this->cValue );
			return $aValue[$nIndex];
		}
	function getValue()
		{
			if ( count( $this->aColValue ) > 1 )
			{
				$aRet = array();
				$aValue = explode ( $this->cSeparatorValue, $this->cValue );
				$nIndex = 0;
				foreach( $this->aColValue as $cColValue )
				{
					$aRet[$cColValue] = $aValue[$nIndex++];
				}
				return $aRet;
			} else
			{
				return $this->cValue;
			}
		}

	}
?>
